package zw.netty.server.Netty.Handler;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.string.StringDecoder;
import zw.netty.server.pojo.Device;

import java.nio.charset.Charset;
import java.util.*;

/**
 & 创建人:zhuwei
 & 邮箱:121203654@qq.com
 & 创建时间:2018-08-27 10:32
 */
public class HexDevoder extends StringDecoder{

/*    private DeviceDataService deviceDataService;
    private DeviceService deviceService;
    public HexDevoder(Map<String,Object> map){
        this.deviceDataService= (DeviceDataService) map.get("deviceDataService");
        this.deviceService= (DeviceService) map.get("deviceService");
    }*/
    public HexDevoder(){

    }
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        String str=msg.toString(Charset.defaultCharset());
        System.out.println(str);
        if(str.indexOf("body")!=-1||str.indexOf("id")!=-1){
            super.decode(ctx, msg, out);
        }else if(str.indexOf("@G")!=-1){
            super.decode(ctx, msg, out);
        }else{
            byte[] receiveMsgBytes = new byte[msg.readableBytes()];
            msg.readBytes(receiveMsgBytes);
            String string=HandUtil.bytesToHexString(receiveMsgBytes);
            Device device=new Device();
            if(string.indexOf("78")!=-1){
                device.setName("ODB");
            }else{

            }

            String ms=HandUtil.DateDecode(receiveMsgBytes);
            device.setId(ctx.channel().id().asShortText());

            device.setBody(ms);
            ByteBuf byteBuf= Unpooled.wrappedBuffer(JSON.toJSONString(device).getBytes());
            super.decode(ctx, byteBuf, out);
        }

    }




}
